package com.scau.os.fileSystem.other;

import com.scau.os.extension.EventSystem;
import com.scau.os.extension.MyDelegate;
import com.scau.os.fileSystem.FolderTree.FolderPane;
import com.scau.os.fileSystem.directory.FileManager;

public enum Command {

    INSTANCE;

    public void launch() {
        EventSystem.instance.register(new MyDelegate("UserInput") {
            @Override
            public void execute(Object data) {
                try {
                    executeCommand((String) data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, true).UnRegisterWhenClose("FileSystem");
    }

    public void executeCommand(String command) {
        try {
            String[] parts = command.split(" ");
            if (parts.length > 1 && parts[1].startsWith("..")) {
                parts[1] = parts[1].substring(2);
                parts[1] = FolderPane.getInstance().getFolderTree().getSelectionModel().getSelectedItem().getValue()
                        .getPath().getAbsolutePath() + parts[1];
            }
            switch (parts[0]) {
                case "create":
                    createFile(parts[1]);
                    break;
                case "delete":
                    removeFile(parts[1]);
                    break;
                case "type":
                    typeFile(parts[1]);
                    break;
                case "copy":
                    copyFile(parts[1], parts[2]);
                    break;
                case "mkdir":
                    createDirectory(parts[1]);
                    break;
                case "rmdir":
                    removeDirectory(parts[1]);
                    break;
                case "chdir":
                    moveDirectory(parts[1], parts[2]);
                    break;
                case "deldir":
                    deleteDirectory(parts[1]);
                    break;
                case "move":
                    moveFile(parts[1], parts[2]);
                    break;
                case "change":
                    changeFileAttributes(parts[1], parts[2]);
                    break;
                case "format":
                    format();
                    break;
                case "rename":
                    changeFileName(parts[1], parts[2]);
                    break;
                case "renamedir":
                    changeDirectoryName(parts[1], parts[2]);
                    break;
                case "execute":
                    execute(parts[1]);
                    break;
                case "demonstrate":
                    demonstrate();
                    break;
                case "help":
                    help();
                    break;
                default:
                    EventSystem.instance.SendEvent("showConsoleInfo", "无效命令");
            }
            EventSystem.instance.SendEvent("diskUpdate", null);
        } catch (MyFileSystemException e) {
            EventSystem.instance.SendEvent("showConsoleInfo", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createFile(String path) throws Exception {
        FileManager.INSTANCE.createFile(path);
    }

    private void removeFile(String path) throws Exception {
        FileManager.INSTANCE.removeFile(path);
    }

    private void typeFile(String path) throws Exception {
        FileManager.INSTANCE.typeFile(path);
    }

    private void copyFile(String sourceFilePath, String destinationDirectoryPath) throws Exception {
        FileManager.INSTANCE.copyFile(sourceFilePath, destinationDirectoryPath);
    }

    private void createDirectory(String path) throws Exception {
        FileManager.INSTANCE.createDirectory(path);
    }

    private void removeDirectory(String path) throws Exception {
        FileManager.INSTANCE.removeDirectory(path);
    }

    private void moveFile(String sourceFile, String destinationDirectory) throws Exception {
        FileManager.INSTANCE.moveFile(sourceFile, destinationDirectory);
    }

    private void changeFileName(String path, String newName) throws Exception {
        FileManager.INSTANCE.changeFileName(path, newName);
    }

    private void moveDirectory(String sourceDirectoryPath, String destinationDirectoryPath) throws Exception {
        FileManager.INSTANCE.moveDirectory(sourceDirectoryPath, destinationDirectoryPath);
    }

    private void deleteDirectory(String path) throws Exception {
        FileManager.INSTANCE.deleteDirectory(path);
    }

    private void format() throws Exception {
        FileManager.INSTANCE.format();
    }

    private void changeFileAttributes(String path, String attributesString) throws Exception {
        FileManager.INSTANCE.changeFileAttributes(path, attributesString);
    }

    private void changeDirectoryName(String path, String newName) throws Exception {
        FileManager.INSTANCE.changeDirectoryName(path, newName);
    }

    private void execute(String path) throws Exception {
        FileManager.INSTANCE.executeFile(path);
    }

    private void demonstrate() throws Exception {
        EventSystem.instance.SendEvent("demonstrationStart", null);
    }

    private void help() {
        EventSystem.instance.SendEvent("showConsoleInfo", "create [文件路径] 创建文件");
        EventSystem.instance.SendEvent("showConsoleInfo", "delete [文件路径] 删除文件");
        EventSystem.instance.SendEvent("showConsoleInfo", "type [文件路径] 查看文件内容");
        EventSystem.instance.SendEvent("showConsoleInfo", "copy [源文件路径] [目标文件夹路径] 复制文件");
        EventSystem.instance.SendEvent("showConsoleInfo", "mkdir [文件夹路径] 创建文件夹");
        EventSystem.instance.SendEvent("showConsoleInfo", "rmdir [文件夹路径] 删除文件夹");
        EventSystem.instance.SendEvent("showConsoleInfo", "chdir [源文件夹路径] [目标文件夹路径] 移动文件夹");
        EventSystem.instance.SendEvent("showConsoleInfo", "deldir [文件夹路径] 删除文件夹");
        EventSystem.instance.SendEvent("showConsoleInfo", "move [源文件路径] [目标文件夹路径] 移动文件");
        EventSystem.instance.SendEvent("showConsoleInfo", "change [文件路径] [属性] 修改文件属性");
        EventSystem.instance.SendEvent("showConsoleInfo", "format 格式化");
        EventSystem.instance.SendEvent("showConsoleInfo", "rename [文件路径] [新文件名] 修改文件名");
        EventSystem.instance.SendEvent("showConsoleInfo", "renamedir [文件夹路径] [新文件夹名] 修改文件夹名");
        EventSystem.instance.SendEvent("showConsoleInfo", "execute [文件路径] 执行文件");
        EventSystem.instance.SendEvent("showConsoleInfo", "demonstrate 演示");
        EventSystem.instance.SendEvent("showConsoleInfo", "help 帮助");
    }
}
